package com.ibm.mall.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ibm.mall.base.BaseAction;
import com.ibm.mall.model.Commodity;
import com.ibm.mall.model.CommodityClass;
import com.ibm.mall.model.PageBean;
import com.ibm.mall.util.FileToByte;
import com.ibm.mall.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class CommodityAction extends BaseAction<Commodity> {

	private static final long serialVersionUID = 1L;
	
	private String commoName;
	
	private File commImage;
	
	private String commImageContentType;
	
	private String commImageFileName;
	
	private Map responseJson;
	
	private Integer commodityClassId;
	
    public Integer getCommodityClassId() {
		return commodityClassId;
	}

	public void setCommodityClassId(Integer commodityClassId) {
		this.commodityClassId = commodityClassId;
	}

	public Map getResponseJson() {   
        return responseJson;   
    }
    
    public void setResponseJson(Map responseJson) {   
        this.responseJson = responseJson;   
    }
	
	public String getCommoName() {
		return commoName;
	}

	public void setCommoName(String commoName) {
		this.commoName = commoName;
	}

	public File getCommImage() {
		return commImage;
	}

	public void setCommImage(File commImage) {
		this.commImage = commImage;
	}

	public String getCommImageContentType() {
		return commImageContentType;
	}

	public void setCommImageContentType(String commImageContentType) {
		this.commImageContentType = commImageContentType;
	}

	public String getCommImageFileName() {
		return commImageFileName;
	}

	public void setCommImageFileName(String commImageFileName) {
		this.commImageFileName = commImageFileName;
	}

	public String list() {
		List<CommodityClass> commodityClassList=commodityClassService.getTopCommodityClass();
		ActionContext.getContext().put("commodityClassList", commodityClassList);
		return "list";
	}
	
	public String add() {
		SimpleDateFormat sf=new SimpleDateFormat();
		CommodityClass commodityClass=commodityClassService.getCommdityClassById(commodityClassId);
		model.setCommodityRemainNumber(model.getCommodityQuantity());
		model.setInsertTime(sf.format(new Date()));
		model.setLastUpdateTime(model.getInsertTime());
		model.setStatus((short)0);
		model.setCommodityClass(commodityClass);
		if(commImage!=null&&commImage.exists()){
			model.setCommodityImage(Hibernate.createBlob(FileToByte.getBytesFromFile(commImage)));
		}
		commodityService.saveCommodity(model);
		return "add";
	}
	
	public String edit() {
		SimpleDateFormat sf=new SimpleDateFormat();
		Commodity commodity=commodityService.getCommodityById(model.getId());
		if(commodityClassId!=null){
			CommodityClass commodityClass=commodityClassService.getCommdityClassById(commodityClassId);
			commodity.setCommodityClass(commodityClass);
		}
		commodity.setLastUpdateTime(sf.format(new Date()));
		if(commImage!=null&&commImage.exists()){
			commodity.setCommodityImage(Hibernate.createBlob(FileToByte.getBytesFromFile(commImage)));
		}
		commodity.setCommodityName(model.getCommodityName());
		commodity.setCommodityPrice(model.getCommodityPrice());
		commodity.setCommodityQuantity(model.getCommodityQuantity());
		commodity.setCommodityManufacturer(model.getCommodityManufacturer());
		commodity.setCommodityDescription(model.getCommodityDescription());
		commodityService.updateCommodity(commodity);
		return "edit";
	}
	
	public String delete() {
		Commodity commodity=commodityService.getCommodityById(model.getId());
		commodity.setStatus((short)1);
		commodityService.updateCommodity(commodity);
		return "delete";
	}
	
	public String getData(){
        Map<String, Object> map = new HashMap<String, Object>();
        QueryHelper queryHelper=null;
        if(commoName!=null&&!commoName.equals("")){
        	queryHelper=new QueryHelper(Commodity.class, "c")//
			.addCondition("c.status=?", (short)0)//查询出未被删除的
			.addCondition("c.commodityName like ?", "%"+commoName+"%")
			.addOrderProperty("c.id", true);
        }else{
        	queryHelper=new QueryHelper(Commodity.class, "c")//
			.addCondition("c.status=?", (short)0)//查询出未被删除的
			.addOrderProperty("c.id", true);
        }
        //分页查询获得pageBean
        PageBean pageBean=commodityService.getPageBean(page,rows,queryHelper);
        map.put("rows", pageBean.getRecordList());
        map.put("total", pageBean.getRecordCount());
        this.setResponseJson(map);
        return "getData";
    }
	
}
